/*
 * Copyright 2014 Davy Maddelein.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.compomics.pepshell.view.dataloading.frames;

import com.compomics.pepshell.view.dataloading.ExperimentFileSelectionPanel;
import com.compomics.pepshell.view.dataloading.MetaDataProviderDialog;

import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Davy Maddelein
 */
public class FileSelectionFrame extends javax.swing.JFrame {

    private final MetaDataProviderDialog metaDataProviderDialog = new MetaDataProviderDialog(this, true);

    /**
     * Creates new form OfflineFileSelectionFrame
     */
    public FileSelectionFrame() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.validate();
        this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        addExperimentMetaDataButton = new javax.swing.JButton();
        launchValidationPanelButton = new javax.swing.JButton();
        referenceExperimentCheckBox = new javax.swing.JCheckBox();
        referenceFileBasedExperimentPanel1 = new com.compomics.pepshell.view.dataloading.ReferenceFileExperimentSelectionPanel();
        experimentFileBasedExperimentSelectionPanel1 = new com.compomics.pepshell.view.dataloading.ExperimentFileSelectionPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("select files");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setForeground(java.awt.Color.white);
        setMinimumSize(new java.awt.Dimension(640, 206));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(480, 330));

        addExperimentMetaDataButton.setText("add metadata...");
        addExperimentMetaDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addExperimentMetaDataButtonActionPerformed(evt);
            }
        });

        launchValidationPanelButton.setText("validate selected files");
        launchValidationPanelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                launchValidationPanelButtonActionPerformed(evt);
            }
        });

        referenceExperimentCheckBox.setBackground(new java.awt.Color(255, 255, 255));
        referenceExperimentCheckBox.setSelected(true);
        referenceExperimentCheckBox.setText("reference experiment in separate file");
        referenceExperimentCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                referenceExperimentCheckBoxActionPerformed(evt);
            }
        });

        referenceFileBasedExperimentPanel1.setPreferredSize(new java.awt.Dimension(378, 84));

        experimentFileBasedExperimentSelectionPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(referenceFileBasedExperimentPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(referenceExperimentCheckBox)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(experimentFileBasedExperimentSelectionPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addExperimentMetaDataButton, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(launchValidationPanelButton, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(referenceExperimentCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(referenceFileBasedExperimentPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(experimentFileBasedExperimentSelectionPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(addExperimentMetaDataButton)
                .addGap(18, 18, 18)
                .addComponent(launchValidationPanelButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void launchValidationPanelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_launchMainWindowButtonActionPerformed

        boolean proceed = true;
        if (referenceFileBasedExperimentPanel1.getReferenceFile() == null && referenceExperimentCheckBox.isSelected()) {
            proceed = false;
            JOptionPane.showMessageDialog(this, "no reference experiment is present, and the reference option was selected, Pepshell cannot continue");
        } else if (proceed && experimentFileBasedExperimentSelectionPanel1.getExperimentFiles().isEmpty()) {
            proceed = false;
            JOptionPane.showMessageDialog(this, "no experiments were selected, Pepshell cannot continue");
        } else {
            ValidationFrame frame;
            if (referenceExperimentCheckBox.isSelected()) {
                frame = new ValidationFrame(referenceFileBasedExperimentPanel1.getReferenceFile(), experimentFileBasedExperimentSelectionPanel1.getExperimentFiles());
            } else {
                frame = new ValidationFrame(experimentFileBasedExperimentSelectionPanel1.getExperimentFiles());
            }
            try {
                frame.setLocationRelativeTo(this);
                frame.setVisible(true);
                this.dispose();
            } catch (NullPointerException npe) {
                JOptionPane.showMessageDialog(this, "could not create the validation window");
            }
        }
    }//GEN-LAST:event_launchMainWindowButtonActionPerformed

    private void addExperimentMetaDataButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addExperimentMetaDataButtonActionPerformed
        // TODO add your handling code here:
        List<File> files = new ArrayList<>();
        files.addAll(experimentFileBasedExperimentSelectionPanel1.getExperimentFiles());
        metaDataProviderDialog.setLocationRelativeTo(this);
        if (referenceExperimentCheckBox.isSelected() && referenceFileBasedExperimentPanel1.getReferenceFile() != null) {
            metaDataProviderDialog.setFilesToAnnotate(referenceFileBasedExperimentPanel1.getReferenceFile(), files);
        } else {
            metaDataProviderDialog.setFilesToAnnotate(files);
        }
        metaDataProviderDialog.setVisible(true);
        if (metaDataProviderDialog.getReferenceExperimentFile() != null) {
            referenceFileBasedExperimentPanel1.setReferenceFile(metaDataProviderDialog.getReferenceExperimentFile());
        }
        experimentFileBasedExperimentSelectionPanel1.setFiles(metaDataProviderDialog.getFiles());
    }//GEN-LAST:event_addExperimentMetaDataButtonActionPerformed


    private void referenceExperimentCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_referenceExperimentCheckBoxActionPerformed
        // TODO add your handling code here:
        if (referenceExperimentCheckBox.isSelected()) {
            referenceFileBasedExperimentPanel1.setVisible(true);
            experimentFileBasedExperimentSelectionPanel1.setMode(ExperimentFileSelectionPanel.ALL_EXPERIMENTS);
        } else {
            referenceFileBasedExperimentPanel1.setVisible(false);
            experimentFileBasedExperimentSelectionPanel1.setMode(ExperimentFileSelectionPanel.REFERENCE_EXPERIMENT);
        }
        this.pack();
    }//GEN-LAST:event_referenceExperimentCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addExperimentMetaDataButton;
    private com.compomics.pepshell.view.dataloading.ExperimentFileSelectionPanel experimentFileBasedExperimentSelectionPanel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton launchValidationPanelButton;
    private javax.swing.JCheckBox referenceExperimentCheckBox;
    private com.compomics.pepshell.view.dataloading.ReferenceFileExperimentSelectionPanel referenceFileBasedExperimentPanel1;
    // End of variables declaration//GEN-END:variables

}
